package com.gko100.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.Article;
import com.gko100.model.Message;
import com.opensymphony.xwork2.ActionContext;

public class MessageAction extends AdminBaseAction {

	private static int pageSize=10;
	private int totalCount=0;
	private int pageNo;
	private int pageCount;

	
	public int getPageSize() {
		return pageSize;
	}
	
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	
	private Message message;
	private List<Message> messageList;
	public Message getMessage() {
		return message;
	}
	public void setMessage(Message message) {
		this.message = message;
	}
	public List<Message> getMessageList() {
		return messageList;
	}
	public void setMessageList(List<Message> messageList) {
		this.messageList = messageList;
	}
	
	public String find_messageList() throws Exception{
		
		
		if(this.getMessageList()!=null){
			this.setMessageList(null);
		}
		if(this.getTotalCount()!=0){
			this.setTotalCount(0);
		}
		
		
		List<Message> messageList = adminManager.findMessageAll();
		if(messageList!=null){
			this.setMessageList(messageList);
		}
		
		if(this.getMessageList()!=null){
			this.setTotalCount(this.getMessageList().size());
			int pageCount = this.getTotalCount()%this.getPageSize();
			if(pageCount!=0){
				this.setPageCount(this.getTotalCount()/this.getPageSize()+1);
			}else{
				this.setPageCount(this.getTotalCount()/this.getPageSize());
			}
		}
		
		if(this.getPageNo()==0){
			this.setPageNo(1);
		}
		if(this.getPageCount()==0){
			this.setPageNo(0);
		}

		if(this.getPageNo()==1){
			List<Message> ml = new ArrayList<Message>();
			if(this.getMessageList()!=null){
				for(int i=0;i<this.getPageSize()&&i<this.getMessageList().size();i++){
					Message m = this.getMessageList().get(i);
					
					if(m!=null){
						ml.add(m);
					}
				}
				this.setMessageList(ml);
				
				if(ml!=null){
					ml=null;
				}
			}
			
		}
		if(this.getPageNo()>1&&this.getPageNo()<=this.getPageCount()){
			
			List<Message> ml = new ArrayList<Message>();
			int firstResult = (this.getPageNo()-1)*this.getPageSize();
			if(this.getMessageList()!=null){
				for(int i=firstResult;i<firstResult+this.getPageSize()&&i<this.getMessageList().size();i++){
					Message m = this.getMessageList().get(i);
					if(m!=null){
						ml.add(m);
					}
				}
				this.setMessageList(ml);
				
				if(ml!=null){
					ml=null;
				}
			}

		}
		
		return "messageList_find_success";
	}
	
	public String find_message() throws Exception{
		
		if(this.getMessage().getMessId()!=null){
			
			Message m = adminManager.findMessageByMId(this.getMessage().getMessId());
			if(m!=null){
				this.setMessage(m);
			}
		}

		return "message_find_success";
	}
	
	public String modify_message() throws Exception {
		
        ActionContext ctx = ActionContext.getContext();   
        HttpServletRequest req = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse res = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
		if(this.getMessage()!=null){
			
			this.getMessage().setReplyFlag(true);
			adminManager.modify_message(this.getMessage());
			res.setContentType("text/html;charset=UTF-8");   
	        PrintWriter w = res.getWriter(); 
	        String tip = "已完成回复!";
	    	w.println(tip);
	    	w.close();
		}
		
		return "message_modify_success";
	}
	
	
	
	
}
